// import formJs from "./regexDetail.js"; //新增弹框配置项
import TableData from "@/components/public/el-table"; //主页面列表
import TableLyout from '@/components/commonPublic/tableLyout.vue'

import {
    getWorkProcessProjectList, 
} from "@/api/request.js";

export default {
    props: [
        "dialogVisible",
    ],
    data() {
        return {
            visible: "",
            loading: false,
            tableData: [],
            isLock: false,
            dialogHeight: null,
            btnShow: true,
            WbsRow: null,
        };
    },
    components: {
        TableLyout,
        TableData,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {

        this.yj_searchForm();
        this.getHeight();
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        closeDialog1() {
            this.$emit("close1");
        },
        // 点击材料
        rowChange(val) {
            this.WbsRow = val
            // this.dataFid = val.fId;
        },
        // 获取列表数据
        yj_searchForm() {
            this.visible = true;
            this.load = true;
            this.userInfo = JSON.parse(sessionStorage.getItem("monitorBack-userInfo"));
            getWorkProcessProjectList({
                companyId: this.userInfo.companyId
                })
                .then((res) => {
                    if (res.errcode == 0) {
                        if(res.data.length!=0){
                            this.tableData =  this.tranListToTreeData1(
                                res.data.workProcessProjectList
                              );
                            this.loading = false;
                        }else {
                        //    this.$message.warning("您暂无WBS树");
                        }
                       
                    } else {
                        this.$message.error(res.msg);
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    this.$message.error(error.msg);
                    this.loadingEditDialog_wbs = false;
                });
        },
        tranListToTreeData1(data) {
            let result = [];
            if (!Array.isArray(data)) {
              return result;
            }
            // 清除已存在的children 重新赋值
            data.forEach((item) => {
              delete item.children;
            });
      
            let map = {};
            data.forEach((item) => {
              map[item.F_CompanyId] = item;
            });
      
            data.forEach((item) => {
              let parent = map[item.F_ParentId];
              if (parent) {
                (parent.children || (parent.children = [])).push(item);
              } else {
                result.push(item);
              }
            });
      
            return result;
          },
        // 点击当前行变色
        handleCurrentChange(val) {
            this.WbsRow = val;
        },
    
        yj_saveForm() {
            console.log(this.WbsRow);
            if (this.WbsRow.children == undefined) {
                this.$emit('getWBS', this.WbsRow)
                this.visible = false
            } else {
                this.$message.error('请先选择工程部位');

            }
        },

    },
};